﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using DTO;
namespace GUI
{
    public partial class ThemHoaDonGUI : Form
    {
        private  static int MaHoaDon;
        private DataSet ds;
        private OleDbDataAdapter da;
        public double ThanhTien
        {
            get { return BUS.CTHoaDonBUS.TongTienHoaDon(MaHoaDon); }
        }
        public double TongCV
        {
            get { return BUS.CTHoaDonBUS.TongCV(MaHoaDon); }
        }
        public ThemHoaDonGUI()
        {
            InitializeComponent();
        }
        public ThemHoaDonGUI(int _MaHoaDon)
        {
            InitializeComponent();
            MaHoaDon = _MaHoaDon;
            lbMaHoaDon.Text = _MaHoaDon.ToString();
        }
        private void ThemHoaDonGUI_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            BUS.SanPhamBUS sp=new BUS.SanPhamBUS();
            cbbsanpham.DataSource = sp.LayTatCaSanPham();
            cbbsanpham.DisplayMember = "TenSP";
            da=BUS.CTHoaDonBUS.LayTatCaCTHoaDon(int.Parse(lbMaHoaDon.Text));
            da.Fill(ds, "CTHoaDon");
            dataGridView1.DataSource = ds.Tables["CTHoaDon"];

            
        }
        private void cbbsanpham_SelectedIndexChanged(object sender, EventArgs e)
        {
            SanPhamDTO sp = (SanPhamDTO)cbbsanpham.SelectedItem;
            txtMaSP.Text = sp.MaSP.ToString();
            txtTenSP.Text = sp.TenSP;
            txtDonGiaTV.Text = sp.DonGiaTV.ToString();
            txtDonGiaKL.Text = sp.DonGiaKL.ToString();
            txtQuyCach.Text = sp.QuyCach;
            txtDVT.Text = sp.DVT;
            txtSLTon.Text = sp.SLTon.ToString();
            txtDiemCV.Text = sp.DiemCV.ToString();
            txtSL.Text = "";
            txtThanhTien.Text = "";
        }
        private void ckbTV_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTV.Checked == true)
            {
                ckbKL.Checked = false;
                //lbMaTV.Visible = true;
                //txtMaTV.Visible = true;
                txtSL.Text = "";
                txtThanhTien.Text = "";
               
            }
            
        }
        private void ckbKL_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKL.Checked == true)
            {
                ckbTV.Checked = false;
                //lbMaTV.Visible = false;
                //txtMaTV.Visible = false;
                txtSL.Text = "";
                txtThanhTien.Text = "";
            }
            
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int sl = int.Parse(txtSL.Text);
                if (ckbTV.Checked == true)
                    sl = sl * int.Parse(txtDonGiaTV.Text);
                else
                    sl = sl * int.Parse(txtDonGiaKL.Text);
                txtThanhTien.Text = sl.ToString();
            }
            catch
            {
                MessageBox.Show("Nhập lại số lượng");
            }
           
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DTO.SanPhamDTO sp = (DTO.SanPhamDTO)cbbsanpham.SelectedItem;
            DataTable dt = ds.Tables["CTHoaDon"];
            DataRow dr = dt.NewRow();
            dr[0] = int.Parse(lbMaHoaDon.Text);
            dr[1] = sp.MaSP;
            dr[2] = 0;
            dr[3] = sp.TenSP;
            dr[4] = int.Parse(txtSL.Text);
            dr[5] = sp.DonGiaKL;
            if(ckbTV.CheckState==CheckState.Checked)
                dr[5] = sp.DonGiaTV;
            dr[6] = int.Parse(txtThanhTien.Text);
            dr[7] = sp.DiemCV;
            ds.Tables["CTHoaDon"].Rows.Add(dr);

        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            da.Update(ds, "CTHoaDon");
        }
    }
}
